package com.cpecc.dao.wuzi;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.HuanHuo;
import com.cpecc.model.wuzi.ShenQing;
import com.cpecc.model.wuzi.WeiXiu;
import com.cpecc.model.wuzi.WuZi;

@Repository
public class HuanHuoDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(HuanHuo huanHuo){
		Session session=sessionFactory.getCurrentSession();
		session.save(huanHuo);
		session.flush();
	}
	
	public HuanHuo get(String id){
		return (HuanHuo) sessionFactory.getCurrentSession().get(HuanHuo.class, id);
	}
	
	public boolean cover(HuanHuo huanHuo){
		String hql="update HuanHuo t set t.yuanChanPinXLH=?,t.xinChanPinXLH=?,t.huanJie=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, huanHuo.getYuanChanPinXLH());
		query.setString(1, huanHuo.getXinChanPinXLH());
		query.setParameter(2, huanHuo.getHuanJie());
		query.setString(3, huanHuo.getId());
		return query.executeUpdate()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<HuanHuo> getAllHuanHuoSQ(int lx){
		String hql="from HuanHuo t where t.huanJie.id=202 and t.liuChengLX=?";		
		Query query=sessionFactory.getCurrentSession().createQuery(hql);		
		query.setInteger(0, lx);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<HuanHuo> getAllHuanHuoSQ2CMR(int lx, User user){
		String hql="from HuanHuo t where t.huanJie.id=202 and t.liuChengLX=? and t.wuZi.caiMaiR=? order by t.shenQing.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, lx);
		query.setParameter(1, user);
		return query.list();
	}
	
	public HuanHuo getHuanHuoByWuZi(WuZi wuZi){
		String hql="from HuanHuo t where t.wuZi.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, wuZi.getId());
		return (HuanHuo) query.list().get(0);
	}
	
	public HuanHuo getByShenQing(ShenQing shenQing){
		String hql="from HuanHuo t where t.shenQing.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, shenQing.getId());
		return (HuanHuo) query.list().get(0);
	}
	
	public HuanHuo getByWeiXiu(WeiXiu weiXiu){
		String hql="from HuanHuo t where t.weiXiu.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, weiXiu.getId());
		return (HuanHuo) query.list().get(0);
	}
	
	public boolean hasWuZi(String wuZiId){
		String hql="from HuanHuo t where t.wuZi.id=? and t.huanJie.id<203";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, wuZiId);
		return query.list().size()>0;
	}
	
	public boolean hasLiuCheng(User user){
		String hql="from HuanHuo t where t.user=? and t.huanJie.id<203";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, user);
		return query.list().size()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<HuanHuo> activeHuanHuo(){
		String hql="from HuanHuo t where t.huanJie<204";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
}
